import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JToolBar;
import javax.swing.JInternalFrame;
import javax.swing.JSplitPane;

import java.awt.FlowLayout;
import java.awt.Color;

import javax.swing.border.BevelBorder;

import java.awt.GridLayout;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JEditorPane;

import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;

import javax.swing.JButton;

import ch.ethz.ssh2.Connection;


public class MainFrame extends JFrame {

	private JPanel contentPane;
	private JTextField adressValue;
	private JTextField usernameValue;
	private JTextField passwordValue;
	private static MainFrame frame = new MainFrame();
	private static SystemInnerFrame innerFrame;
	private String hostName;
	private String hostUser = "root";
	private String hostSecret = "kingstone_703";

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					//frame = new MyFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		setTitle("\u767B\u5F55\u8FDC\u7A0B\u7CFB\u7EDF");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 302, 233);
		contentPane = new JPanel();
		contentPane.setBackground(Color.LIGHT_GRAY);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel adress = new JLabel("\u8FDC\u7A0B\u5730\u5740\uFF1A");
		adress.setForeground(Color.DARK_GRAY);
		adress.setFont(new Font("宋体", Font.BOLD, 12));
		adress.setBounds(32, 24, 71, 15);
		contentPane.add(adress);
		
		adressValue = new JTextField();
		adressValue.setBounds(97, 21, 146, 21);
		contentPane.add(adressValue);
		adressValue.setColumns(10);
		
		JLabel username = new JLabel("\u8FDC\u7A0B\u7528\u6237\uFF1A");
		username.setForeground(Color.DARK_GRAY);
		username.setFont(new Font("宋体", Font.BOLD, 12));
		username.setBounds(32, 60, 71, 15);
		contentPane.add(username);
		
		usernameValue = new JTextField();
		usernameValue.setColumns(10);
		usernameValue.setBounds(97, 57, 146, 21);
		contentPane.add(usernameValue);
		
		JLabel password = new JLabel("\u8FDC\u7A0B\u5BC6\u7801\uFF1A");
		password.setForeground(Color.DARK_GRAY);
		password.setFont(new Font("宋体", Font.BOLD, 12));
		password.setBounds(32, 98, 71, 15);
		contentPane.add(password);
		
		passwordValue = new JTextField();
		passwordValue.setColumns(10);
		passwordValue.setBounds(97, 95, 146, 21);
		contentPane.add(passwordValue);
		
		JButton login = new JButton("\u767B\u5F55");
		login.setBounds(150, 141, 93, 23);
		contentPane.add(login);
		
		login.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e){
				hostName = adressValue.getText();
				String[] host = hostName.split(":");
				RemoteShellTool.port = Integer.parseInt(host[1]);
				if(usernameValue.getText().isEmpty()) {
					hostUser = "root";
				}else {
					hostUser = usernameValue.getText();
				}
				if(passwordValue.getText().isEmpty()) {
					hostSecret = "kingstone_703";
				}else {
					hostSecret = passwordValue.getText();
				}
				RemoteShellTool sshTool = new RemoteShellTool(host[0], hostUser, hostSecret, null);
				innerFrame = new SystemInnerFrame(sshTool);
				try {
					if(sshTool.login()){
						frame.dispose();
						JOptionPane.showMessageDialog(null, "登录成功！","提示", JOptionPane.INFORMATION_MESSAGE);
						innerFrame.setVisible(true);
					}else{
						JOptionPane.showMessageDialog(null, "登录失败！请重试","提示", JOptionPane.INFORMATION_MESSAGE);
					}
				} catch (IOException e1) {
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "登录失败！请重试","提示", JOptionPane.INFORMATION_MESSAGE);
				}
			}
		});
	}
}
